// package: rewords-server-2024
// filename: cCfg
// author: diogoxiang@gmail.com
// time: 2024/7/31 上午10:56

package cfg

import (
	"context"
	"github.com/gogf/gf/v2/frame/g"
	"rewords-server-2024/internal/library/apptoken"
	"rewords-server-2024/internal/model"
)

var (
	// GConfig 单例
	GConfig *Config
	//RootPath string
)

func init() {

	var (
		config     Config
		setingConf *SettingConfig
		mongoConf  *MongoConf
		mailConf   *MailConf
		qiniuConf  *QiniuConf
		//wxConf     *WxConf
		//actsignConf *Actsign
		dingConf *DingConf

		tokenConf *model.TokenConf

		ctx context.Context
	)

	c := g.Config()

	// setting
	err := c.MustGet(ctx, "server").Scan(&setingConf)
	if err != nil {
		g.Log().Info(ctx, err)
	}
	config.Setting = *setingConf

	// mongodb
	err = c.MustGet(ctx, "mongoConf").Scan(&mongoConf)
	if err != nil {
		g.Log().Info(ctx, err)
	}

	config.Mongo = *mongoConf

	// gmail
	err = c.MustGet(ctx, "email").Scan(&mailConf)
	if err != nil {
		g.Log().Info(ctx, err)
	}
	config.Mail = *mailConf

	// qiniu
	err = c.MustGet(ctx, "qiniu").Scan(&qiniuConf)
	if err != nil {
		g.Log().Info(ctx, err)
	}
	config.Qiniu = *qiniuConf

	// WxConf
	//err = c.MustGet(ctx, "wxConfig").Scan(&wxConf)
	//if err != nil {
	//	g.Log().Info(ctx, err)
	//}
	//config.Weixin = *wxConf

	//err = c.MustGet(ctx, "actsign").Scan(&actsignConf)
	//if err != nil {
	//	g.Log().Info(ctx, err)
	//}
	//config.Actsign = *actsignConf

	err = c.MustGet(ctx, "DingConfig").Scan(&dingConf)
	if err != nil {
		g.Log().Info(ctx, err)
	}
	config.Ding = *dingConf

	err = c.MustGet(ctx, "tokenConf").Scan(&tokenConf)
	if err != nil {
		g.Log().Info(ctx, err)
	}
	config.Token = *tokenConf

	// apptoken 初始化
	apptoken.SetConfig(tokenConf)

	// end config
	GConfig = &config

	g.Dump(GConfig)
}
